#include "emixer_gui.h"
#include "emixer_alsa.h"
#include "emixer_callbacks.h"


static Ewl_Widget *win = NULL;
static Ewl_Widget *vbox = NULL;
static Emixer_Elem *emixer_gui_elems = NULL;
static Ewl_Widget *menu_bar=NULL;

int emixer_gui_init()
{
    // create the window
    win = ewl_window_new();
    ewl_window_title_set ( EWL_WINDOW ( win ), "Emixer" );
    ewl_window_name_set ( EWL_WINDOW ( win ), "emixer" );
    ewl_window_class_set ( EWL_WINDOW ( win ), "emixer" );
    ewl_callback_append ( win, EWL_CALLBACK_DELETE_WINDOW, destroy_cb, NULL );
    ewl_widget_show ( win );

    // create the v container
    vbox = ewl_vbox_new();
    ewl_container_child_append ( EWL_CONTAINER ( win ), vbox );
    ewl_object_fill_policy_set ( EWL_OBJECT ( vbox ), EWL_FLAG_FILL_ALL );
    ewl_object_padding_set ( EWL_OBJECT ( vbox ), 1, 10, 1, 10 );
    ewl_widget_show ( vbox );

    // build an empty menu
    menu_bar = ewl_hmenubar_new();
    ewl_container_child_append(EWL_CONTAINER(vbox),menu_bar);
    //ewl_object_fill_policy_set(EWL_OBJECT(menu_bar),EWL_FLAG_FILL_??);
    //ewl_object_alignment_set(EWL_OBJECT(menu_bar),EWL_FLAG_ALIGN_LEFT);
    ewl_widget_show(menu_bar);

    return 0;
}


int emixer_gui_build()
{
    assert(win && vbox);

    Ewl_Widget* card_label = NULL;
    Ewl_Widget* slider_box = NULL;
    Ewl_Widget* slider_label = NULL;
    Ewl_Widget* slider = NULL;

    // create the text label
    card_label = ewl_text_new();
    ewl_container_child_append ( EWL_CONTAINER ( vbox ), card_label );
    ewl_text_styles_set ( EWL_TEXT ( card_label ), EWL_TEXT_STYLE_SOFT_SHADOW );
    ewl_text_color_set ( EWL_TEXT ( card_label ), 255, 0, 0, 255 );
    ewl_object_padding_set ( EWL_OBJECT ( card_label ), 7, 7, 7, 7 );
    ewl_object_fill_policy_set ( EWL_OBJECT ( card_label ), EWL_FLAG_FILL_NONE );

    char buf[256];
    strcpy ( buf, "Card : " );
    strcat ( buf, emixer_card_name_get() );
    strcat ( buf, " : " );
    strcat ( buf, emixer_device_name_get() );

    ewl_text_text_set ( EWL_TEXT ( card_label ), buf );
    ewl_widget_show ( card_label );

    // the grid
    slider_box = ewl_grid_new();
    ewl_grid_dimensions_set ( EWL_GRID ( slider_box ), 2, 1 );
    ewl_container_child_append ( EWL_CONTAINER ( vbox ), slider_box );
    ewl_grid_column_preferred_w_use(EWL_GRID(slider_box), 0 );
    ewl_widget_show ( slider_box );

    // build internal data structs
    int i, num = emixer_elem_num_get();
    if ( emixer_gui_elems!=0 )
        free ( emixer_gui_elems );
    emixer_gui_elems = ( Emixer_Elem* ) malloc( num*sizeof( Emixer_Elem ) );

    // build the volume sliders
    long min, max, now;
    for ( i = 0;i < num;++i )
    {
        emixer_gui_elems[i].elem_id=i;
        emixer_gui_elems[i].elem_is_muted = emixer_elem_mute_get(i,0);
        if ( emixer_elem_is_active ( i ) )
        {
            emixer_gui_elems[i].elem_is_active = 1;
            if ( emixer_elem_vol_range_get ( i, &min, &max ) < 0 )
            {
                emixer_gui_elems[i].elem_is_visible = 0;
                emixer_gui_elems[i].elem_has_range = 0;
                slider = NULL;
                slider_label = NULL;
                continue;
            }

            emixer_gui_elems[i].elem_is_visible = 1;
            emixer_gui_elems[i].elem_has_range = 1;

            // the label
            slider_label = ewl_label_new();
            ewl_container_child_append ( EWL_CONTAINER ( slider_box ), slider_label );
            ewl_object_padding_set ( EWL_OBJECT ( slider_label ), 7, 1, 1, 1 );
            ewl_label_text_set ( EWL_LABEL ( slider_label ), emixer_elem_name_get ( i ) );
            ewl_widget_show ( slider_label );

            // the slider
            emixer_elem_vol_now_get ( i, 0, &now );
            slider = ewl_hseeker_new();
            ewl_container_child_append ( EWL_CONTAINER ( slider_box ), slider );
            ewl_object_padding_set ( EWL_OBJECT ( slider ), 1, 5, 1, 1 );
            ewl_object_fill_policy_set ( EWL_OBJECT ( slider ), EWL_FLAG_FILL_ALL );
            ewl_range_minimum_value_set ( EWL_RANGE ( slider ), 0 );
            ewl_range_maximum_value_set ( EWL_RANGE ( slider ), 100 );

            float vol = (100.0f/(max-min))*now;
            ewl_range_value_set ( EWL_RANGE ( slider ), vol);
            ewl_callback_append(slider,EWL_CALLBACK_VALUE_CHANGED,mixer_upt_cb,(void*)i);
            ewl_widget_show ( slider );
        }
    }

    return 0;
}

int
emixer_gui_menu()
{
    assert(win && vbox && menu_bar && emixer_gui_elems);

    Ewl_Widget* main=NULL;
    Ewl_Widget* menu=NULL;
    Ewl_Widget* elem=NULL;
    Ewl_Widget* item=NULL;

    main = ewl_menu_new();
    ewl_button_label_set(EWL_BUTTON(main),"Menu");
    ewl_container_child_append(EWL_CONTAINER(menu_bar),main);
    ewl_object_fill_policy_set(EWL_OBJECT(main),EWL_FLAG_FILL_SHRINK);
    ewl_widget_show(main);

    menu = ewl_menu_new();
    ewl_button_label_set(EWL_BUTTON(menu),"Elements");
    ewl_container_child_append(EWL_CONTAINER(main),menu);
    ewl_object_fill_policy_set(EWL_OBJECT(menu),EWL_FLAG_FILL_ALL);
    ewl_widget_show(menu);

    item = ewl_menu_item_new();
    ewl_button_label_set(EWL_BUTTON(item),"Quit");
    ewl_callback_append(item,EWL_CALLBACK_CLICKED,destroy_cb,NULL);
    ewl_container_child_append(EWL_CONTAINER(main),item);
    ewl_object_fill_policy_set(EWL_OBJECT(item),EWL_FLAG_FILL_ALL);
    ewl_widget_show(item);

    // populate the menu
    int i;
    int num = emixer_elem_num_get();
    for (i=0;i<num;++i)
    {
        // no mute control, so skip it
        if(emixer_gui_elems[i].elem_is_muted<0)
            continue;

        elem = ewl_menu_new();
        ewl_button_label_set(EWL_BUTTON(elem),emixer_elem_name_get(i));
        ewl_container_child_append(EWL_CONTAINER(menu),elem);
        ewl_object_fill_policy_set(EWL_OBJECT(elem),EWL_FLAG_FILL_ALL);
        ewl_widget_show(elem);

        item = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(item),"Mute");
        ewl_container_child_append(EWL_CONTAINER(elem),item);
        ewl_object_fill_policy_set(EWL_OBJECT(item),EWL_FLAG_FILL_ALL);
        ewl_callback_append(item,EWL_CALLBACK_CLICKED,menu_mute_cb,
                                                (void*)&(emixer_gui_elems[i]));
        ewl_widget_show(item);
    }

    return 0;
}

int
emixer_gui_deinit()
{
    // clean up
    if (emixer_gui_elems!=NULL)
        free(emixer_gui_elems);
}


